package bean;

import java.io.IOException;
import java.io.Serializable;
import java.util.Date;

import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.faces.bean.*;
import javax.faces.context.FacesContext;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.apache.commons.codec.digest.DigestUtils;

import controller.UserMgr;
import dto.ElementoBaseDTO;
import dto.PacchettoPersonaleDTO;
import dto.UtenteDTO;

@ManagedBean
@ViewScoped
public class UserBean implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@EJB
	private UserMgr userMgr;
	
	@Resource(lookup = "EMailME")
	private Session mailSession;
	
	private UtenteDTO utenteModifica;
	private String emailReset;
	private PacchettoPersonaleDTO selectedPacchetto;
	private ElementoBaseDTO selectedElemento;
	private String password;
	
	public int loggato(){
		return userMgr.loggato();
	}
	
	public UtenteDTO getUtenteModifica() {
		return utenteModifica;
	}

	public void setUtenteModifica(UtenteDTO utenteModifica) {
		this.utenteModifica = utenteModifica;
	}

	public UtenteDTO getUtente() {
		return userMgr.getUtenteDTO();
	}

	public PacchettoPersonaleDTO getSelectedPacchetto() {
		return selectedPacchetto;
	}

	public void setSelectedPacchetto(PacchettoPersonaleDTO selectedPacchetto) {
		this.selectedPacchetto = selectedPacchetto;
	}

	public ElementoBaseDTO getSelectedElemento() {
		return selectedElemento;
	}

	public void setSelectedElemento(ElementoBaseDTO selectedElemento) {
		this.selectedElemento = selectedElemento;
	}
	
	public String valida(String email){
		if(email!=null)
		userMgr.valida(email);
		try {
			FacesContext.getCurrentInstance().getExternalContext().redirect("http://localhost:8080/TravelDreamOnlineWeb/user/index.xhtml?userb=true");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "home?faces-redirect=true";
	}	
	
	public void check(){
		if(this.userMgr.check()){
			try {
				FacesContext.getCurrentInstance().getExternalContext().redirect("http://localhost:8080/TravelDreamOnlineWeb/user/index.xhtml");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else{
		    FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
			try {
				FacesContext.getCurrentInstance().getExternalContext().redirect("http://localhost:8080/TravelDreamOnlineWeb/notvalid.xhtml");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public void loadData(){
		this.utenteModifica = getUtente();
	}
	
	public void onEdit(){
		System.out.println("Un cazzo");{
		utenteModifica.setPassword(DigestUtils.sha256Hex(utenteModifica.getPassword()));
		this.userMgr.update(utenteModifica);}
	}
	
	public void sendEmail(String to, String subject, String body) {
		
		MimeMessage message = new MimeMessage(mailSession);
		try {
		message.setFrom(new InternetAddress(mailSession.getProperty("mail.from")));
		InternetAddress[] address = {new InternetAddress(to)};
		message.setRecipients(MimeMessage.RecipientType.TO, address);
		message.setSubject(subject);
		message.setSentDate(new Date());
		message.setText(body);
		Transport.send(message);
		} catch (MessagingException ex) {
		ex.printStackTrace();
		}
		}
	
	public void resetPassword(){
		final String email = this.emailReset;

		if(email!=null){
		final String newp;
		newp = userMgr.resetPassword(email);
		Thread t = new Thread(new Runnable() {
	         public void run()
	         {
	        	 sendEmail(email,"Recupero password","La tua password è stata resettata. La nuova password è: "+newp);
	         }
	});
		t.start();
		}
		try {
			FacesContext.getCurrentInstance().getExternalContext().redirect("http://localhost:8080/TravelDreamOnlineWeb/user/index.xhtml");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public String getEmailReset() {
		return emailReset;
	}

	public void setEmailReset(String emailReset) {
		this.emailReset = emailReset;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	
}
